package com.pro.service.impl;

import com.pro.entry.*;
import com.pro.mapper.JsCourseMapper;
import com.pro.service.JsCourseService;
import com.pro.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.logging.LogManager;
import java.util.stream.IntStream;

/**
 * @Author 谭金诚
 * @Description TODO
 * @Date 2022/4/15 11:26
 * @Version 1.0
 */
@Service
public class JsCourseServiceImple implements JsCourseService {

    @Autowired
    private JsCourseMapper mapper;

    @Autowired
    RedisCache redisCache;

    @Override
    public List<JsCourse> getCourses(Integer currentPage, Integer pageSize, Integer courseStatus, Integer courseCstatus) {
       /* List<JsCourse> jsCourse = redisCache.getCacheList("jsCourse");
        if (!jsCourse.isEmpty()){
            return jsCourse;
        }*/
       //数据库分页
       currentPage = (currentPage - 1) * pageSize;
        courseStatus = 2;//上架
        //courseCstatus = 4;//审核通过
       List<JsCourse> courses = mapper.getCourses(currentPage,pageSize,courseStatus,courseCstatus);
        //redisCache.setCacheList("jsCourse",courses);
        return courses;
    }

    @Override
    public JsCourse getCourseDetails(JsCourse course) {
        course.setCourseStatus(2);//上架
        //course.setCourseCstatus(4);//审核通过
        JsCourse courseDetails = mapper.getCourseDetails(course);
        return courseDetails;
    }

    @Override
    public List<JsCourse> getCourseMenu(Integer userId) {
        List<JsCourse> courseMenu = mapper.getCourseMenu(userId);
        return courseMenu;
    }

    @Override
    public List<JsComment> getCourseComments(Integer courseId, Integer type) {
        List<JsComment> courseComments = mapper.getCourseComments(courseId, type);
        return courseComments;
    }

    @Override
    public Integer getCourseCommentsNums(Integer courseId, Integer type) {
        Integer commNums = mapper.getCourseCommentsNums(courseId, type);
        return commNums;
    }

    @Override
    public List<Integer> getPraisedComms() {
        List<Integer> praisedComms = mapper.getPraisedComms();
        return praisedComms;
    }

    @Override
    @Transactional
    public Integer getPraisedCommsLog(JsCommentLike commentLike) {
        Integer praisedCommsLog = mapper.getPraisedCommsLog(commentLike);
        if (praisedCommsLog == null){
            commentLike.setCreateDate(new Date());
            boolean flag = mapper.addPraisedCommsLog(commentLike);
            if (flag){
                mapper.updateFabulous("+1", commentLike.getCommentId());//点赞
                praisedCommsLog = 1;
                return praisedCommsLog;
            }else{
                praisedCommsLog = 0;
                return praisedCommsLog;
            }
        }else{
            boolean flag = mapper.delPraisedCommsLog(commentLike);
            if (flag){
                boolean b1 = mapper.updateFabulous("-1", commentLike.getCommentId());//取消点赞
                praisedCommsLog = 2;
                return praisedCommsLog;
            }else{
                praisedCommsLog = 0;
                return praisedCommsLog;
            }
        }
    }

    @Override
    public boolean addCourseComment(JsComment comment) {
        comment.setType(2);//评论类型：课程评论
        comment.setUserType("1");//用户类型：前台
        comment.setStatus("1");//评论状态：正常
        comment.setParentId(0);//父级评论id
        comment.setDisplay(2);//是否显示：2显示
        comment.setFabulous(0);//点赞数
        comment.setReply(0);//回复数
        comment.setCommentDate(new Date());//评论时间
        comment.setCreateDate(comment.getCommentDate());
        boolean flag = mapper.addCourseComment(comment);
        return flag;
    }

    @Override
    public JsFabulous getPraiseCourse(JsFabulous fabulous) {
        JsFabulous jsFabulous = mapper.getPraiseCourse(fabulous);
        return jsFabulous;
    }

    @Override
    public JsUserCollection getCollectionCourse(JsUserCollection userCollection) {
        JsUserCollection collectionCourse = mapper.getCollectionCourse(userCollection);
        return collectionCourse;
    }

    @Override
    public boolean addPraiseCourse(JsFabulous fabulous) {
        fabulous.setLikeTime(new Date());
        boolean flag = mapper.addPraiseCourse(fabulous);
        mapper.updataPraiseCourse("like_num","+1",fabulous.getBePraisedId());
        return flag;
    }

    @Override
    public boolean delPraiseCourse(JsFabulous fabulous) {
        boolean flag = mapper.delPraiseCourse(fabulous);
        mapper.updataPraiseCourse("like_num","-1",fabulous.getBePraisedId());
        return flag;
    }

    @Override
    public boolean addCollectionCourse(JsUserCollection userCollection) {
        userCollection.setCollectionDate(new Date());
        boolean flag = mapper.addCollectionCourse(userCollection);
        mapper.updataPraiseCourse("collect_num","+1",userCollection.getProductId());
        return flag;
    }

    @Override
    public boolean delCollectionCourse(JsUserCollection userCollection) {
        boolean flag = mapper.delCollectionCourse(userCollection);
        mapper.updataPraiseCourse("collect_num","-1",userCollection.getProductId());
        return flag;
    }

    @Override
    public List<JsSearchKeywords> getHotKeywords(Integer num) {
        num = 16;
        List<JsSearchKeywords> hotKeywords = mapper.getHotKeywords(num);
        return hotKeywords;
    }
}
